package sskj.regulatory.presentation.screen.setup

import android.annotation.TargetApi
import android.content.Intent
import android.os.Build
import android.support.annotation.RequiresApi
import android.widget.Toast
import com.alipay.sdk.app.statistic.c.v
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView
import com.vondear.rxtools.RxDataTool
import kotlinx.android.synthetic.main.fragment_setup.view.*
import kotlinx.android.synthetic.main.setup_myinfo_layout.view.*
import sskj.regulatory.R
import sskj.regulatory.data.response.session.IndexDataResponse
import sskj.regulatory.presentation.base_mvp.base.BaseFragment
import sskj.regulatory.presentation.constans.Constans
import sskj.regulatory.presentation.screen.authentication.projectdepartment.ProjectDeptFragment
import sskj.regulatory.presentation.screen.home.SetupContract
import sskj.regulatory.presentation.screen.home.SetupPresenter
import javax.inject.Inject
import com.qmuiteam.qmui.widget.QMUILoadingView
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView.newSection
import sskj.regulatory.presentation.screen.launcher.LauncherActivity
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIFollowRefreshOffsetCalculator
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIDefaultRefreshOffsetCalculator
import com.vondear.rxtools.view.RxToast
import sskj.regulatory.presentation.screen.authentication.video.VideoFragment


class SetupFragment : BaseFragment<SetupContract.View, SetupContract.Presenter>(), SetupContract.View {


    override fun initPresenter() = mPresenter

    override val layoutResId: Int = R.layout.fragment_setup

    override fun onBackPressed(): Boolean = true

    @Inject
    lateinit var mPresenter: SetupPresenter

    override fun injectDependencies() {
        activityComponent.inject(this)
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initView() {
        initTopBar()
        initPullTtoRefresh()
    }

    private fun initPullTtoRefresh() {
        mCacheView.pullTtoRefresh.setRefreshOffsetCalculator(QMUIDefaultRefreshOffsetCalculator())

        mCacheView.pullTtoRefresh.setOnPullListener(object : QMUIPullRefreshLayout.OnPullListener {
            override fun onMoveTarget(offset: Int) {

            }

            override fun onMoveRefreshView(offset: Int) {

            }

            override fun onRefresh() {
                mPresenter.indexData()
            }
        })
    }

    fun refresh() {
        mPresenter.indexData()
    }

    override fun showError(message: String?) {
        QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle("错误提示")
                .setMessage(message)
                .addAction("确定") { dialog, _ -> dialog.dismiss() }
                .create(R.style.QMUI_Dialog).show()
    }

    private fun initTopBar() {
        mCacheView.sTopBar.setTitle("我的");
        mCacheView.sTopBar.setBackgroundDividerEnabled(false)
    }

    override fun setupWithData(indexDataResponse: IndexDataResponse) {
        mCacheView.sNameTv.text = indexDataResponse.data?.simpleUser?.name ?: "佚名"
        mCacheView.sPhoneTv.text = RxDataTool.hideMobilePhone4(indexDataResponse.data?.simpleUser?.username
                ?: "13000000000")
        initGroupListView(indexDataResponse)
        mCacheView.pullTtoRefresh.finishRefresh()
    }

    private fun initGroupListView(indexDataResponse: IndexDataResponse) {

        mCacheView.sGroupListView.removeAllViews()

        val signedItem = mCacheView.sGroupListView.createItemView("CA证书认证")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            signedItem.setImageDrawable(resources.getDrawable(R.mipmap.ic_ca, resources.newTheme()))
        }
        signedItem.orientation = QMUICommonListItemView.HORIZONTAL
        signedItem.detailText = "未认证"
        signedItem.textView.textSize = 14f
        signedItem.accessoryType = QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON

        val videoItem = mCacheView.sGroupListView.createItemView("监控视频认证")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            videoItem.setImageDrawable(resources.getDrawable(R.mipmap.ic_sp, resources.newTheme()))
        }
        videoItem.orientation = QMUICommonListItemView.HORIZONTAL
        videoItem.accessoryType = QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON
        videoItem.textView.textSize = 14f
        videoItem.detailText = when (indexDataResponse.isBingDingRole(Constans.JK_ROLE_ID)) {
            true -> "已认证"
            false -> "未认证"
        }

        val pmItem = mCacheView.sGroupListView.createItemView("扬尘监控认证")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            pmItem.setImageDrawable(resources.getDrawable(R.mipmap.ic_yc, resources.newTheme()))
        }
        pmItem.orientation = QMUICommonListItemView.HORIZONTAL
        pmItem.detailText = "未认证"
        pmItem.accessoryType = QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON
        pmItem.textView.textSize = 14f


        val cmisItem = mCacheView.sGroupListView.createItemView("综合监管系统认证")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            cmisItem.setImageDrawable(resources.getDrawable(R.mipmap.ic_zhjg, resources.newTheme()))
        }
        cmisItem.orientation = QMUICommonListItemView.HORIZONTAL
        cmisItem.detailText = "未认证"
        cmisItem.accessoryType = QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON
        cmisItem.textView.textSize = 14f


        val projectDepItem = mCacheView.sGroupListView.createItemView("项目部")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            projectDepItem.setImageDrawable(resources.getDrawable(R.mipmap.ic_xmb, resources.newTheme()))
        }
        projectDepItem.orientation = QMUICommonListItemView.HORIZONTAL
        projectDepItem.detailText = when (indexDataResponse.isBingDingRole(Constans.XMB_ROLE_ID)) {
            true -> "已认证"
            false -> "未认证"
        }
        projectDepItem.accessoryType = QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON
        projectDepItem.textView.textSize = 14f


        QMUIGroupListView.newSection(context)
                .setTitle("权限认证")
                .setDescription("")
                .addItemView(cmisItem, { v ->
                    when (v) {
                        is QMUICommonListItemView -> RxToast.showToastShort("建设中!")
                    }
                })
                .addItemView(videoItem, { v ->
                    when (v) {
                        is QMUICommonListItemView -> startFragment(VideoFragment())
                    }
                })
                .addItemView(projectDepItem, { v ->
                    when (v) {
                        is QMUICommonListItemView -> startFragment(ProjectDeptFragment())
                    }
                })
                .addItemView(signedItem, { v ->
                    when (v) {
                        is QMUICommonListItemView -> RxToast.showToastShort("建设中!")
                    }
                })
                .addItemView(pmItem, { v ->
                    when (v) {
                        is QMUICommonListItemView -> RxToast.showToastShort("建设中!")

                    }
                })
                .addTo(mCacheView.sGroupListView)


        val itemLogout = mCacheView.sGroupListView.createItemView("注销")
        itemLogout.accessoryType = QMUICommonListItemView.ACCESSORY_TYPE_CUSTOM
        itemLogout.orientation = QMUICommonListItemView.HORIZONTAL
        itemLogout.textView.textSize = 14f
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            itemLogout.setImageDrawable(resources.getDrawable(R.mipmap.ic_zx, resources.newTheme()))
        }

        QMUIGroupListView.newSection(context)
                .setTitle("")
                .addItemView(itemLogout, { v ->
                    when (v) {
                        is QMUICommonListItemView -> mPresenter.logOut()
                    }
                })
                .addTo(mCacheView.sGroupListView);
    }

    override fun logOutOk() {
        activity.tipDialog = QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("注销成功")
                .create()

        activity.tipDialog?.show()

        view?.postDelayed({
            activity.tipDialog?.dismiss()
            startActivity(Intent(context, LauncherActivity::class.java))
            getActivity()?.finish()
        }, 1500)


    }

    override fun showLoading() {
        activity.tipDialog = QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("正在加载")
                .create()
        activity.tipDialog?.show()
    }

    override fun hideLoading() {
        activity.tipDialog?.dismiss()
    }


}